<template>
  <div class="table-box">
    <ProTable
      ref="proTableRef"
      title="排班时间表"
      :indent="20"
      :columns="columns"
      :search-columns="searchColumns"
      :request-api="getTableList"
      row-key="id"
    >
      <!-- 表格 header 按钮 -->
      <template #tableHeader="scope">
        <el-button type="primary" v-auth="'vehicle.schedule.create'" :icon="CirclePlus" @click="openAddEdit('新增排班时间表')">
          新增
        </el-button>
        <el-button
          v-auth="'vehicle.schedule.remove'"
          type="danger"
          :icon="Delete"
          plain
          :disabled="!scope.isSelected"
          @click="batchDelete(scope.selectedListIds)"
        >
          批量删除
        </el-button>
        <!-- <el-button
          v-auth="'vehicle.schedule.import'"
          type="primary"
          :icon="Upload"
          plain
          @click="importData"
        >
          导入
        </el-button>
        <el-button
          v-auth="'vehicle.schedule.export'"
          type="primary"
          :icon="Download"
          plain
          @click="downloadFile"
        >
          导出
        </el-button> -->
      </template>
      <template #operation="{ row }">
        <el-button
          v-auth="'vehicle.schedule.update'"
          type="primary"
          link
          :icon="EditPen"
          @click="openAddEdit('编辑排班时间表', row, false)"
        >
          编辑
        </el-button>
        <el-button v-auth="'vehicle.schedule.remove'" type="primary" link :icon="Delete" @click="deleteInfo(row)">
          删除
        </el-button>
      </template>
    </ProTable>
    <VehicleScheduleForm ref="vehicleScheduleRef" />
    <ImportExcel ref="ImportExcelRef" />
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { CirclePlus, Delete, EditPen, Upload, Download } from '@element-plus/icons-vue';
import ProTable from '@/components/ProTable/index.vue';
import {
  createVehicleScheduleApi,
  removeVehicleScheduleApi,
  updateVehicleScheduleApi,
  getVehicleScheduleListApi,
  getVehicleScheduleDetailApi,
  importVehicleScheduleExcelApi,
  exportVehicleScheduleExcelApi
} from '@/api/modules/vehicleschedule/vehicleSchedule';
import { useHandleData } from '@/hooks/useHandleData';
import VehicleScheduleForm from '@/views/vehicleschedule/vehicleSchedule/components/VehicleScheduleForm.vue';
import { useDictOptions } from '@/hooks/useDictOptions';
import type { ColumnProps, ProTableInstance, SearchProps } from '@/components/ProTable/interface';
import type { VehicleScheduleQuery, VehicleScheduleRow } from '@/api/types/vehicleschedule/vehicleSchedule';
import ImportExcel from '@/components/ImportExcel/index.vue';
import { downloadTemplate } from '@/api/modules/system/common';
import { useDownload } from '@/hooks/useDownload';
import { useDict } from '@/hooks/useDict';
defineOptions({
  name: 'VehicleScheduleView'
});
// 使用useDict Hook 主动加载字典
useDict(['dynamic_user_options']);
const proTableRef = ref<ProTableInstance>();
// 表格配置项
const columns: ColumnProps<VehicleScheduleRow>[] = [
  { type: 'selection', width: 80 },
  { prop: 'title', label: '排班计划名称' },
  {
    prop: 'scheduleType',
    label: '排班类型',
    tag: true,
    enum: useDictOptions('schedule_type'),
    fieldNames: {
      label: 'codeName',
      value: 'id',
      tagType: 'callbackShowStyle'
    }
  },
  { prop: 'time1', label: '排班时间' },
  { prop: 'time2', label: '排班时间' },
  { prop: 'time3', label: '排班时间' },
  { prop: 'sort', label: '排序' },
  // { prop: 'startTime', label: '上班时间' },
  // { prop: 'endTime', label: '下班时间' },
  { prop: 'createTime', label: '创建时间' },
  { prop: 'updateTime', label: '更新时间' },
  {
    prop: 'createId',
    label: '创建人ID',
    tag: true,
    enum: useDictOptions('dynamic_user_options'),
    fieldNames: {
      label: 'codeName',
      value: 'id',
      tagType: 'callbackShowStyle'
    }
  },
  {
    prop: 'updateId',
    label: '更新人ID',
    tag: true,
    enum: useDictOptions('dynamic_user_options'),
    fieldNames: {
      label: 'codeName',
      value: 'id',
      tagType: 'callbackShowStyle'
    }
  },
  { prop: 'operation', label: '操作', width: 250, fixed: 'right' }
];
// 搜索条件项
const searchColumns: SearchProps[] = [
  { prop: 'title', label: '排班计划名称', el: 'input' },
  // {
  //   prop: 'startTime',
  //   label: '上班时间',
  //   el: 'date-picker',
  //   span: 2,
  //   props: {
  //     type: 'datetimerange',
  //     valueFormat: 'YYYY-MM-DD HH:mm:ss'
  //   }
  // },
  // {
  //   prop: 'endTime',
  //   label: '下班时间',
  //   el: 'date-picker',
  //   span: 2,
  //   props: {
  //     type: 'datetimerange',
  //     valueFormat: 'YYYY-MM-DD HH:mm:ss'
  //   }
  // },
  {
    prop: 'scheduleType',
    label: '排班类型',
    el: 'select',
    enum: useDictOptions('schedule_type'),
    fieldNames: {
      label: 'codeName',
      value: 'id',
      tagType: 'callbackShowStyle'
    }
  }
];
// 获取table列表
const getTableList = (params: VehicleScheduleQuery) => {
  let newParams = formatParams(params);
  return getVehicleScheduleListApi(newParams);
};
const formatParams = (params: VehicleScheduleQuery) => {
  let newParams = JSON.parse(JSON.stringify(params));
  if (newParams.startTime) {
    newParams.startTimeStart = newParams.startTime[0];
    newParams.startTimeEnd = newParams.startTime[1];
    delete newParams.startTime;
  }

  if (newParams.endTime) {
    newParams.endTimeStart = newParams.endTime[0];
    newParams.endTimeEnd = newParams.endTime[1];
    delete newParams.endTime;
  }

  if (newParams.createTime) {
    newParams.createTimeStart = newParams.createTime[0];
    newParams.createTimeEnd = newParams.createTime[1];
    delete newParams.createTime;
  }

  if (newParams.updateTime) {
    newParams.updateTimeStart = newParams.updateTime[0];
    newParams.updateTimeEnd = newParams.updateTime[1];
    delete newParams.updateTime;
  }

  if (newParams.deleteTime) {
    newParams.deleteTimeStart = newParams.deleteTime[0];
    newParams.deleteTimeEnd = newParams.deleteTime[1];
    delete newParams.deleteTime;
  }

  return newParams;
};
// 打开 drawer(新增、查看、编辑)
const vehicleScheduleRef = ref<InstanceType<typeof VehicleScheduleForm>>();
const openAddEdit = async (title: string, row: any = {}, isAdd = true) => {
  if (!isAdd) {
    const record = await getVehicleScheduleDetailApi({ id: row?.id });
    row = record?.data;
  }
  const params = {
    title,
    row: { ...row },
    api: isAdd ? createVehicleScheduleApi : updateVehicleScheduleApi,
    getTableList: proTableRef.value?.getTableList
  };
  vehicleScheduleRef.value?.acceptParams(params);
};
// 删除信息
const deleteInfo = async (params: VehicleScheduleRow) => {
  await useHandleData(removeVehicleScheduleApi, { ids: [params.id] }, `删除【${params.id}】排班时间表`);
  proTableRef.value?.getTableList();
};
// 批量删除信息
const batchDelete = async (ids: (string | number)[]) => {
  await useHandleData(removeVehicleScheduleApi, { ids }, '删除所选排班时间表');
  proTableRef.value?.clearSelection();
  proTableRef.value?.getTableList();
};
// 导入
const ImportExcelRef = ref<InstanceType<typeof ImportExcel>>();
const importData = () => {
  const params = {
    title: '排班时间表',
    templateName: '排班时间表',
    tempApi: downloadTemplate,
    importApi: importVehicleScheduleExcelApi,
    getTableList: proTableRef.value?.getTableList
  };
  ImportExcelRef.value?.acceptParams(params);
};
// 导出
const downloadFile = async () => {
  let newParams = formatParams(proTableRef.value?.searchParam as VehicleScheduleQuery);
  useDownload(exportVehicleScheduleExcelApi, '排班时间表', newParams);
};
</script>
